package kz.pompei.ifacer.main.align;

import static java.awt.GridBagConstraints.HORIZONTAL;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import kz.pompei.ifacer.conf.Config;
import kz.pompei.ifacer.main.AbstractAction1;
import kz.pompei.ifacer.main.ActionManager;
import kz.pompei.ifacer.main.SizeLocationSaver;
import kz.pompei.ifacer.main.WindowState;

public class AlignFrame extends JFrame {
  
  public AlignFrame(Config mainConfig, JFrame parent) {
    setDefaultCloseOperation(HIDE_ON_CLOSE);
    
    new SizeLocationSaver(this, "AlignFrame", mainConfig, parent.getX() + parent.getWidth(),
        parent.getY(), 100, parent.getHeight(), WindowState.NORMAL);
  }
  
  public void setToggleButton(final JToggleButton toggleButton) {
    toggleButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        setVisible(toggleButton.isSelected());
      }
    });
    
    addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        toggleButton.setSelected(false);
      }
    });
  }
  
  private JPanel pane;
  private GridBagConstraints c;
  
  public final void init(ActionManager m) {
    pane = new JPanel(new GridBagLayout());
    setContentPane(pane);
    c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 5, 5);
    c.gridy = 0;
    
    {
      c.gridx = 0;
      addButton(m.alignLeft, "alignLeft.png", "Выровнять по левой стороне");
      c.gridx = 1;
      addButton(m.alignRight, "alignRight.png", "Выровнять по правой стороне");
      c.gridy++;
    }
    {
      c.gridx = 0;
      addButton(m.alignTop, "alignTop.png", "Выровнять по верхней стороне");
      c.gridx = 1;
      addButton(m.alignBottom, "alignBottom.png", "Выровнять по нижней стороне");
      c.gridy++;
    }
    {
      c.gridx = 0;
      addButton(m.alignWidths, "alignWidths.png", "Выровнять ширины");
      c.gridx = 1;
      addButton(m.alignHeights, "alignHeights.png", "Выровнять высоты");
      c.gridy++;
    }
    {
      c.gridx = 0;
      addButton(m.alignBetweensVert, "alignBetweensVert.png", "Выровнять промежутки");
      c.gridx = 1;
      addButton(m.alignToVerticalCenter, "alignCenter.png", "Выровнять по центру");
      c.gridy++;
    }
    {
      c.gridx = 0;
      c.gridwidth = 2;
      c.fill = HORIZONTAL;
      addButton(m.alignBetweensHoriz, "alignBetweensHoriz.png", "Выровнять промежутки");
      c.gridy++;
    }
  }
  
  private void addButton(AbstractAction1 action, String picture, String hint) {
    JButton b = action == null ? new JButton() :new JButton(action);
    b.setToolTipText(hint);
    b.setIcon(new ImageIcon(getClass().getResource(picture)));
    pane.add(b, c);
  }
  
  @SuppressWarnings("unused")
  private void addButton(String picture, String hint) {
    addButton(null, picture, hint);
  }
}
